home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / snip0493.zip / NORESET.C < prev    next >
C/C++ Source or Header  |  1993-04-05  |  3KB  |  104 lines

  1. /* code to disable <Ctrl><Alt><Del>.                                */
  2. /* Compiled and tested under TC++ and MSC 6                         */
  3.  
  4. #include <dos.h>
  5. #include <conio.h>
  6.  
  7. #if defined(__TURBOC__)
  8.  #define _interrupt interrupt
  9.  #define _far far
  10. #else                              /* i.e. if MSC                   */
  11.  #define inportb(port) inp(port)
  12.  #define outportb(port,val) outp(port,val)
  13.  #define enable() _enable()
  14.  #define disable() _disable()
  15.  #define getvect(int) _dos_getvect(int)
  16.  #define setvect(int,ptr) _dos_setvect(int,ptr)
  17.  unsigned char peekb(unsigned seg, unsigned ofs)
  18.  {
  19.          unsigned char far *ptr;
  20.  
  21.          FP_SEG(ptr) = seg;
  22.          FP_OFF(ptr) = ofs;
  23.          return *ptr;
  24.  }
  25. #endif
  26.  
  27. #define CTRLALT        (0x08|0x04) /* bit flags set in kbstat()     */
  28. #define DELSCAN        0x53        /* keyboard scan code for <Del>  */
  29. #define KEYPORT        0x60        /* keyboard scan code port       */
  30. #define CONTROLLERPORT 0x20        /* interrupt controller port     */
  31. #define kbstat()       peekb(0,0x417)  /* BIOS data area - kb flags */
  32.  
  33. #define keyport()      inportb(KEYPORT)
  34.         /* macro that returns the scancode of the key that caused   */
  35.         /* the interrupt                                            */
  36.  
  37. #define install()      (oldkbisr=getvect(0x09),setvect(0x09,newkbisr))
  38.         /* installation macro, installs newkbisr() in the keyboard  */
  39.         /* interrupt chain                                          */
  40.  
  41. #define uninstall()       setvect(0x09,oldkbisr)
  42.         /* removal macro, call to remove newkbisr() from interrupt  */
  43.         /* chain.  oldkbisr()  must be removed before program ends  */
  44.  
  45. void (_interrupt _far * oldkbisr)(void);
  46.         /* address of old keyboard ISR                              */
  47.  
  48. void _interrupt _far newkbisr(void)
  49. {
  50.    if((keyport()==DELSCAN)&&((kbstat()&CTRLALT)==CTRLALT))
  51.    {
  52.        char kbin = (char)inportb(KEYPORT+1);      /* reset keyboard */
  53.  
  54.        outportb(KEYPORT+1, kbin|0x80);
  55.        outportb(KEYPORT+1, kbin);
  56.        disable();
  57.        outportb(CONTROLLERPORT,0x20); /* tell controller to shut up */
  58.        enable();
  59.    }
  60.    else
  61.        oldkbisr();  /* chain to old keyboard isr */
  62. }
  63.  
  64. #ifdef TEST
  65.  
  66. #include <stdio.h>
  67. #include <stdlib.h>
  68. #include <conio.h>
  69. #include <signal.h>
  70. #include <errno.h>
  71.  
  72. void main(void)
  73. {
  74.       int ch = 0;
  75.       void cleanup(void), cbrk(int);
  76.  
  77.       install();
  78.       atexit(cleanup);
  79.  
  80.       signal(SIGINT, cbrk);
  81.  
  82.       puts("This is a test of Ctrl-Alt-Del disabling.");
  83.       puts("Press any key, but only Esc should stop this program.");
  84.  
  85.       while (0x1b != ch)
  86.       {
  87.             if (kbhit)
  88.                   ch = getch();
  89.       }
  90. }
  91.  
  92. void cbrk(int sig)
  93. {
  94.       signal(SIGINT, SIG_IGN);
  95.       signal(SIGINT, cbrk);
  96. }
  97.  
  98. void cleanup(void)
  99. {
  100.       uninstall();
  101. }
  102.  
  103. #endif
  104.